﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;
using BestBuy.Common;

namespace BestBuy.PageUser
{
    public partial class MbBuying : System.Web.UI.Page
    {
        cBangTinMua cBangTinMua = new cBangTinMua();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["IDDN"].ToString().Equals(""))
            {
                Response.Redirect("~");
            }
            if (!IsPostBack)
            {
                LoadGV();
            }
        }

        private void LoadGV()
        {
            int userID = int.Parse(Session["IDDN"].ToString());
            gridview.DataSource = cBangTinMua.GetAllByUserID(userID);
            gridview.DataBind();
            if (gridview.Rows.Count > 0)
            {
                lbtnXoa.Visible = true;
            }
        }

        protected void lbtnXoa_Click(object sender, EventArgs e)
        {
            int size = gridview.Rows.Count;
            for (int i = 0; i <= size - 1; i++)
            {
                try
                {
                    CheckBox pXoa = (CheckBox)gridview.Rows[i].FindControl("chkXoa");
                    if (pXoa.Checked)
                    {
                        int ID = Convert.ToInt32(((Label)gridview.Rows[i].FindControl("lbIDBangTinMua")).Text);
                        cBangTinMua.RemoveAt(ID);
                    }
                }
                catch { }
            }
            LoadGV();
        }

        protected void gridview_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int status = (int)DataBinder.Eval(e.Row.DataItem, "TinhTrang");
                int IDBangTinMua = (int)DataBinder.Eval(e.Row.DataItem, "IDBangTinMua");
                switch (status)
                {
                    case (int)BangTinMuaStatus.Active:
                        HyperLink link = e.Row.FindControl("HyperLinkEdit") as HyperLink;
                        if (link != null)
                        {
                            link.Visible = true;
                            link.NavigateUrl = "EditBuyOffer.aspx?IDBangTinMua=" + IDBangTinMua;
                        }
                        break;
                    case (int)BangTinMuaStatus.Unverified:
                        Label lbUnverified = e.Row.FindControl("lbUnverified") as Label;
                        if (lbUnverified != null)
                        {
                            lbUnverified.Visible = true;
                        }
                        break;
                }
                //string datePost = (string)DataBinder.Eval(e.Row.DataItem, "NgayDang");
                //string dateExpiry = (string)DataBinder.Eval(e.Row.DataItem, "ThoiHanDang");
                //if (datePost != null)
                //{
                //    DateTime date = DateTime.Parse(datePost);
                //    date = date.AddDays(dateExpiry);
                //    Label lbThoiHanDang = e.Row.FindControl("lbThoiHanDang") as Label;
                //    if (lbThoiHanDang != null)
                //    {
                //        lbThoiHanDang.Text = date.ToShortDateString();
                //    }
                //}
            }
        }
    }
}